package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.example.mapper.RecordBasicMapper;
import org.example.model.common.dtos.PageResponseDto;
import org.example.model.common.dtos.record.RecordBasicDto;
import org.example.model.common.vos.record.SearchRecordVo;
import org.example.model.record.RecordBasic;
import org.example.service.RecordBasicService;
import org.example.service.UserBasicService;
import org.example.utils.PageUtils;
import org.example.utils.UserIdThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 日志管理
 */
@Service
@Slf4j
@Transactional
public class RecordBasicServiceImpl extends ServiceImpl<RecordBasicMapper, RecordBasic> implements RecordBasicService {

    @Autowired
    private RecordBasicMapper recordBasicMapper;

    @Autowired
    private UserBasicService userBasicService;

    /**
     * 新增日志记录
     *
     * @param recordBasic
     */
    @Override
    public Boolean InsertRecordBasic(RecordBasic recordBasic) {
        if (recordBasic == null) return false;
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return false;
        recordBasic.setUserId(userId);
        recordBasic.setLoginAccount(userBasicService.SelectUserById(userId).getLoginAccount());
        recordBasic.setCreateDate(LocalDateTime.now());
        return recordBasicMapper.insert(recordBasic) > 0;
    }

    /**
     * 根据条件（登录账号模糊查询，创建时间范围查询）分页查询
     *
     * @param searchRecordVo
     * @param pageIndex
     * @param pageSize
     */
    @Override
    public PageResponseDto<List<RecordBasicDto>> SelectPageByConditions(SearchRecordVo searchRecordVo, int pageIndex, int pageSize) {
        LambdaQueryWrapper<RecordBasic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(searchRecordVo.getLoginAccount()),RecordBasic::getLoginAccount,searchRecordVo)
                .orderByDesc(RecordBasic::getCreateDate);
        List<RecordBasic> recordBasics = recordBasicMapper.selectList(queryWrapper);
        List<RecordBasicDto> recordBasicDtos = new ArrayList<>();
        RecordBasicDto recordBasicDto = null;
        for (RecordBasic recordBasic : recordBasics) {
            recordBasicDto = this.SelectRecordBasicById(recordBasic.getId());
            recordBasicDtos.add(recordBasicDto);
        }
        PageResponseDto<List<RecordBasicDto>> pageResponseDto = new PageResponseDto<>();
        pageResponseDto.setSize(recordBasicDtos.size());
        pageResponseDto.setData(new PageUtils<RecordBasicDto>().page(recordBasicDtos,pageIndex,pageSize));
        return pageResponseDto;
    }

    //分析请求类型和请求方法得出完整请求路径
    private RecordBasicDto SelectRecordBasicById(int id)
    {
        RecordBasic recordBasic = recordBasicMapper.selectById(id);
        RecordBasicDto recordBasicDto = new RecordBasicDto();
        BeanUtils.copyProperties(recordBasic,recordBasicDto);
        recordBasicDto.setUserName(userBasicService.SelectUserById(recordBasicDto.getUserId()).getUsername());
        return recordBasicDto;
    }
}
